--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[

]]--
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------ 界面调试
    local Widget = require "widgets/widget"
    local Image = require "widgets/image" -- 引入image控件
    local UIAnim = require "widgets/uianim"
    local Screen = require "widgets/screen"
    local AnimButton = require "widgets/animbutton"
    local ImageButton = require "widgets/imagebutton"
    local Menu = require "widgets/menu"
    local Text = require "widgets/text"
    local TEMPLATES = require "widgets/redux/templates"
    local ScrollableList = require "widgets/scrollablelist"
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- buff图标数据
    local debuffs_data = {
        -------------------------------------------------------------
        --- buff持续时间-彼岸蝶舞
            ["hutao_com_butterfly_s_embrace"] = {
                atlas = "images/widgets/hutao_inspect_buff_icon.xml",
                image = "hutao_com_butterfly_s_embrace.tex",
                test_fn = function(inst) -- 是否显示图标。
                    -- return inst.replica.hutao_com_inspect_data:Get("hutao_com_butterfly_s_embrace",0) > 0
                    -- return true
                    return inst.replica.hutao_com_butterfly_s_embrace:GetDebuffRemainTime() > 0
                end,
                create_fn = function(inst,line,root) -- 创建图标 和 更新函数
                    local str = line:AddChild(Text(CODEFONT,45,"    ",{ 0/255 , 0/255 ,0/255 , 1}))
                    str:SetPosition(120,0)
                    local update_fn = function()
                        -- local num = inst.replica.hutao_com_inspect_data:Get("hutao_com_butterfly_s_embrace",0)
                        local num = inst.replica.hutao_com_butterfly_s_embrace:GetDebuffRemainTime()
                        if num > 0 then
                            --- 保留一位小数
                            str:SetString(string.format("%.1f",num))
                        else
                            root.inst:PushEvent("scroll_box_refresh")
                        end
                    end
                    update_fn()
                    -- line.inst:ListenForEvent("hutao_classified_inspect_data_update",update_fn,inst)
                    line.inst:ListenForEvent("hutao_com_butterfly_s_embrace_update",update_fn,inst)
                end,
            },
        -------------------------------------------------------------
        --- buff-肉感（梅菜扣肉）
            ["hutao_debuff_braised_pork_belly_with_preserved_vegetable"] = {
                atlas = "images/widgets/hutao_inspect_buff_icon.xml",
                image = "hutao_debuff_braised_pork_belly_with_preserved_vegetable.tex",
                test_fn = function(inst) -- 是否显示图标。
                    return inst.replica.hutao_com_inspect_data:Get("hutao_debuff_braised_pork_belly_with_preserved_vegetable",0) > 0
                end,
                create_fn = function(inst,line,root) -- 创建图标 和 更新函数
                    local str = line:AddChild(Text(CODEFONT,45,"    ",{ 0/255 , 0/255 ,0/255 , 1}))
                    str:SetPosition(120,0)
                    local update_fn = function()
                        local num = inst.replica.hutao_com_inspect_data:Get("hutao_debuff_braised_pork_belly_with_preserved_vegetable",0)
                        if num > 0 then
                            --- 保留一位小数
                            str:SetString(string.format("%.1f",num))
                        else
                            root.inst:PushEvent("scroll_box_refresh")
                        end
                    end
                    update_fn()
                    line.inst:ListenForEvent("hutao_classified_inspect_data_update",update_fn,inst)
                end,
            },
        -------------------------------------------------------------
        --- buff-通窍（幽幽大行军）
            ["hutao_debuff_ghostly_march"] = {
                atlas = "images/widgets/hutao_inspect_buff_icon.xml",
                image = "hutao_debuff_ghostly_march.tex",
                test_fn = function(inst) -- 是否显示图标。
                    return inst.replica.hutao_com_inspect_data:Get("hutao_debuff_ghostly_march",0) > 0
                end,
                create_fn = function(inst,line,root) -- 创建图标 和 更新函数
                    local str = line:AddChild(Text(CODEFONT,45,"    ",{ 0/255 , 0/255 ,0/255 , 1}))
                    str:SetPosition(120,0)
                    local update_fn = function()
                        local num = inst.replica.hutao_com_inspect_data:Get("hutao_debuff_ghostly_march",0)
                        if num > 0 then
                            --- 保留一位小数
                            str:SetString(string.format("%.1f",num))
                        else
                            root.inst:PushEvent("scroll_box_refresh")
                        end
                    end
                    update_fn()
                    line.inst:ListenForEvent("hutao_classified_inspect_data_update",update_fn,inst)
                end,
            },
        -------------------------------------------------------------
        --- buff-酗酒（梅花酿）
            ["hutao_debuff_plum_blossom_wine"] = {
                atlas = "images/widgets/hutao_inspect_buff_icon.xml",
                image = "hutao_debuff_plum_blossom_wine.tex",
                test_fn = function(inst) -- 是否显示图标。
                    return inst.replica.hutao_com_inspect_data:Get("hutao_debuff_plum_blossom_wine",0) > 0
                end,
                create_fn = function(inst,line,root) -- 创建图标 和 更新函数
                    local str = line:AddChild(Text(CODEFONT,45,"    ",{ 0/255 , 0/255 ,0/255 , 1}))
                    str:SetPosition(120,0)
                    local update_fn = function()
                        local num = inst.replica.hutao_com_inspect_data:Get("hutao_debuff_plum_blossom_wine",0)
                        if num > 0 then
                            --- 保留一位小数
                            str:SetString(string.format("%.1f",num))
                        else
                            root.inst:PushEvent("scroll_box_refresh")
                        end
                    end
                    update_fn()
                    line.inst:ListenForEvent("hutao_classified_inspect_data_update",update_fn,inst)
                end,
            },
        -------------------------------------------------------------
        --- debuff-不朽尸王
            ["hutao_debuff_undying_lich"] = {
                atlas = "images/widgets/hutao_inspect_buff_icon.xml",
                image = "hutao_debuff_undying_lich.tex",
                test_fn = function(inst) -- 是否显示图标。
                    return inst.replica.hutao_com_inspect_data:Get("hutao_debuff_undying_lich",0) > 0
                end,
                create_fn = function(inst,line,root) -- 创建图标 和 更新函数
                    local str = line:AddChild(Text(CODEFONT,45,"    ",{ 0/255 , 0/255 ,0/255 , 1}))
                    str:SetPosition(120,0)
                    local update_fn = function()
                        local num = inst.replica.hutao_com_inspect_data:Get("hutao_debuff_undying_lich",0)
                        if num > 0 then
                            --- 保留一位小数
                            str:SetString(string.format("%.1f",num))
                        else
                            root.inst:PushEvent("scroll_box_refresh")
                        end
                    end
                    update_fn()
                    line.inst:ListenForEvent("hutao_classified_inspect_data_update",update_fn,inst)
                end,
            },
        -------------------------------------------------------------
        --- debuff-信
            ["hutao_debuff_undying_lich_after_wakeup"] = {
                atlas = "images/widgets/hutao_inspect_buff_icon.xml",
                image = "hutao_debuff_undying_lich_after_wakeup.tex",
                test_fn = function(inst) -- 是否显示图标。
                    return inst.replica.hutao_com_inspect_data:Get("hutao_debuff_undying_lich_after_wakeup",0) > 0
                end,
                create_fn = function(inst,line,root) -- 创建图标 和 更新函数
                    local str = line:AddChild(Text(CODEFONT,45,"    ",{ 0/255 , 0/255 ,0/255 , 1}))
                    str:SetPosition(120,0)
                    local update_fn = function()
                        local num = inst.replica.hutao_com_inspect_data:Get("hutao_debuff_undying_lich_after_wakeup",0)
                        if num > 0 then
                            --- 保留一位小数
                            str:SetString(string.format("%.1f",num))
                        else
                            root.inst:PushEvent("scroll_box_refresh")
                        end
                    end
                    update_fn()
                    line.inst:ListenForEvent("hutao_classified_inspect_data_update",update_fn,inst)
                end,
            },
        -------------------------------------------------------------
        --- buff-蝶隐之时
            ["hutao_debuff_vanishing_butterfly_moment"] = {
                atlas = "images/widgets/hutao_inspect_buff_icon.xml",
                image = "hutao_debuff_vanishing_butterfly_moment.tex",
                test_fn = function(inst) -- 是否显示图标。
                    return inst.replica.hutao_com_inspect_data:Get("hutao_debuff_vanishing_butterfly_moment",0) > 0
                end,
                create_fn = function(inst,line,root) -- 创建图标 和 更新函数
                    local str = line:AddChild(Text(CODEFONT,45,"    ",{ 0/255 , 0/255 ,0/255 , 1}))
                    str:SetPosition(120,0)
                    local update_fn = function()
                        local num = inst.replica.hutao_com_inspect_data:Get("hutao_debuff_vanishing_butterfly_moment",0)
                        if num > 0 then
                            --- 保留一位小数
                            str:SetString(string.format("%.1f",num))
                        else
                            root.inst:PushEvent("scroll_box_refresh")
                        end
                    end
                    update_fn()
                    line.inst:ListenForEvent("hutao_classified_inspect_data_update",update_fn,inst)
                end,
            },
        -------------------------------------------------------------
    }
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 创建API
    TUNING.HUTAO_INSPECT_HUD_FNS.buff_box_create = function(inst,front_root)
    --- 滚动条区域创建函数。得到区域位置，和所有slot
        --------------------------------------------------------------------------
        --- 基础
            local root = front_root:AddChild(Widget())
            local atlas = "images/widgets/hutao_custom_scroll_bar.xml"
            root:SetPosition(570,70)
        --------------------------------------------------------------------------
        --- 创建区域API
            local function create_scroll_box_and_get_slots(slots_num)
                --------------------------------------------------------------------------
                ---- 行数计算
                    local lines_num = slots_num or 8
                --------------------------------------------------------------------------
                --- 单行操作
                    local line_items_box = {}
                    local all_slots = {}
                    local function create_line()
                        local line = Widget()
                        -- local test_icon = line:AddChild(Image("images/widgets/hutao_inspect_buff_icon.xml","hutao_com_butterfly_s_embrace.tex"))
                        table.insert(all_slots,line)
                        return line
                    end
                --------------------------------------------------------------------------
                --- 滚动条区域创建
                    for i = 1, lines_num, 1 do
                        table.insert(line_items_box,create_line())
                    end
                    local listwidth = 500
                    local listheight = 350
                    local itemheight = 1
                    local itempadding = 70
                    local updatefn = function() end
                    local widgetstoupdate = nil
                    local scroll_bar_area = root:AddChild(ScrollableList(line_items_box,listwidth, listheight, itemheight, itempadding,updatefn,widgetstoupdate))
                    scroll_bar_area:SetPosition(0,0) -- 设置滚动区域位置
                    scroll_bar_area.scroll_bar_container:SetPosition(-260,10)  --- 设置滚动条位置
                    ---- 设置滚动条样式
                    scroll_bar_area.up_button:SetTextures(atlas,"arrow_scrollbar_up.tex")
                    scroll_bar_area.down_button:SetTextures(atlas,"arrow_scrollbar_down.tex")
                    scroll_bar_area.scroll_bar_line:SetTexture(atlas,"scrollbarline.tex")
                    scroll_bar_area.position_marker:SetTextures(atlas,"scrollbarbox.tex","scrollbarbox.tex","scrollbarbox.tex","scrollbarbox.tex","scrollbarbox.tex")
                    scroll_bar_area.position_marker:OnGainFocus() --- 不知道为什么，贴图替换失败。只能用这种方式刷一下。
                    scroll_bar_area.position_marker:OnLoseFocus()
                --------------------------------------------------------------------------

                --------------------------------------------------------------------------

                return scroll_bar_area,all_slots
            end
        --------------------------------------------------------------------------
        --- 生成滚动条区域
            -- local scroll_bar_area,all_slots = create_scroll_box_and_get_slots(0)
        --------------------------------------------------------------------------
        --- 检查当前的debuff数量,并生成滚动条区域
            root.inst:ListenForEvent("scroll_box_refresh",function()
                if root.scroll_bar_area then
                    root.scroll_bar_area:Kill()
                    root.scroll_bar_area = nil
                end
                local activing_debuffs = {}
                for debuff_index, data_fn in pairs(debuffs_data) do
                    if data_fn.test_fn(inst) then
                        table.insert(activing_debuffs,debuff_index)
                    end
                end
                if #activing_debuffs == 0 then
                    return
                end
                local scroll_bar_area,all_slots = create_scroll_box_and_get_slots(#activing_debuffs)
                for k, line in pairs(all_slots) do
                    local debuff_index = activing_debuffs[k]
                    local debuff_data = debuffs_data[debuff_index]
                    local icon = line:AddChild(ImageButton(debuff_data.atlas,
                        debuff_data.image,
                        debuff_data.image,
                        debuff_data.image,
                        debuff_data.image,
                        debuff_data.image
                    ))
                    icon.focus_scale = {1.0, 1.0, 1.0}
                    icon.ongainfocus = function()
                        front_root.inst:PushEvent("set_info",debuff_index)
                    end
                    debuff_data.create_fn(inst,line,root)
                end
                root.scroll_bar_area = scroll_bar_area
            end)
            root.inst:PushEvent("scroll_box_refresh")
        --------------------------------------------------------------------------
    end
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------